package com.ncloud.system.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.system.entity.Company;
import com.ncloud.system.mapper.CompanyMapper;
import com.ncloud.system.mapper.SequenceMapper;
import com.ncloud.system.service.CompanyService;
import com.ncloud.system.service.UserService;
@Service
public class CompanyServiceImpl implements CompanyService{
	@Resource
	private CompanyMapper companyMapper;
	@Resource
	private SequenceMapper sequenceMapper;
	@Resource
	private UserService userService;
	
	@Override
	public JSONArray getCompanyInfo(JSONObject json) {
		Integer state = json.getInteger("state");
		List<Company> companys = companyMapper.getCompanyInfo(state);
		JSONArray results=JSON.parseArray(JSON.toJSONString(companys));
		return results;
	}

	@Override
	public int insertCompany(Company company) {
		Integer companyId = sequenceMapper.getSequence("company_id").intValue();
		company.setCompanyId(companyId);
		company.setState(0);
		return companyMapper.insertCompany(company);
	}

	@Override
	public int updateCompany(Company company) {
		return companyMapper.updateCompany(company);
	}

	@Override
	public int deleteCompany(Integer companyId) {
		return companyMapper.deleteCompany(companyId);
	}

	@Override
	public int updateCompanyState(Company company) {
		if(company.getState().intValue()==1){
			userService.lockUserByCompany(Long.valueOf(company.getCompanyId()));
		}else if(company.getState().intValue()==0){
			userService.unLockUserByCompany(Long.valueOf(company.getCompanyId()));
		}
		return companyMapper.updateCompanyState(company);
	}

	@Override
	public Integer getCompanyStateById(Integer companyId) {
		List<Company> companys = companyMapper.getCompanyById(companyId);
		if(companys!=null && companys.size()>0)
		{
			return companys.get(0).getState();
		}
		return 0;
	}
}
